/*
 * @Author: LuZeng
 * @Date: 2022-08-01 18:42:58
 * @LastEditTime: 2022-08-27 15:42:45
 * @LastEditors: LuZeng
 * @Description: 小白本白，写的不好多多包涵！！！
 * @FilePath: \jsd:\rjiananzhuang\WEB\WEB workspace\实训三\练习\ThreeNode\app\app\controller\logon.js
 * 别乱动！
 */
// 登录模块
let { Controller } = require("egg");

let Response = require("../utils/Response");

class UserLogon extends Controller {
  // 登录
  async logon() {
    let { ctx, service, app } = this;
    // 1、获取前端参数
    let accountData = ctx.request.body;
    // 2、调用service层操作数据库
    let accountInfo = await service.logon.logon(accountData);
    // 3、给出响应
    // 4、签发token
    // let token =
    //   "Bearer " +
    //   app.jwt.sign({ username: accountInfo.username }, app.config.jwt.secret, {
    //     expiresIn: "60s",
    //   });
    // 示例
    const token =
      "Bearer " +
      this.app.jwt.sign(
        { username: accountData.username },
        this.app.config.jwt.secret,
        { expiresIn: "1200s" }
      );

    // const { username } = app.jwt.verify(token, this.app.config.jwt.secret);
    ctx.body = new Response("登录成功", token);
  }

  // 注册
  async register() {
    let { ctx, service } = this;
    // 获取参数
    let userInfo = ctx.request.body;
    // 调用service层方法
    let message = await service.logon.register(userInfo);
    if (message == 1) {
      ctx.body = new Response("注册成功");
    } else {
      ctx.body = new Response("用户已存在，请直接登录");
    }
  }
}
module.exports = UserLogon;
